package com.ruoyi.xzs.service.impl;

import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.xzs.contant.XZSContent;
import com.ruoyi.xzs.domain.KnowsTree;
import com.ruoyi.xzs.mapper.KnowsQuestionMapper;
import com.ruoyi.xzs.mapper.KnowsTreeMapper;
import com.ruoyi.xzs.mapper.QuestionMapper;
import com.ruoyi.xzs.service.IKnowsTreeService;
import com.ruoyi.xzs.vo.KnowAndQuestionVO;
import com.ruoyi.xzs.vo.KnowQuestionVO;
import com.ruoyi.xzs.vo.KnowsIdAndQuestionIdVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 知识点树Service业务层处理
 * 
 * @author li
 * @date 2023-03-14
 */
@Service
public class KnowsTreeServiceImpl implements IKnowsTreeService 
{
    @Autowired
    private KnowsTreeMapper knowsTreeMapper;

    @Autowired
    private KnowsQuestionMapper knowsQuestionMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 查询知识点树
     * 
     * @param Id 知识点树主键
     * @return 知识点树
     */
    @Override
    public KnowsTree selectKnowsTreeById(Long Id)
    {
        return knowsTreeMapper.selectKnowsTreeById(Id);
    }

    /**
     * 查询知识点树列表
     * 
     * @param knowsTree 知识点树
     * @return 知识点树
     */
    @Override
    public List<KnowsTree> selectKnowsTreeList(KnowsTree knowsTree)
    {
        return knowsTreeMapper.selectKnowsTreeList(knowsTree);
    }

    /**
     * 新增知识点树
     * 
     * @param knowsTree 知识点树
     * @return 结果
     */
    @Override
    public int insertKnowsTree(KnowsTree knowsTree)
    {
        return knowsTreeMapper.insertKnowsTree(knowsTree);
    }

    /**
     * 修改知识点树
     * 
     * @param knowsTree 知识点树
     * @return 结果
     */
    @Override
    public int updateKnowsTree(KnowsTree knowsTree)
    {
        return knowsTreeMapper.updateKnowsTree(knowsTree);
    }

    /**
     * 批量删除知识点树
     * 
     * @param ids 需要删除的知识点树主键
     * @return 结果
     */
    @Override
    public int deleteKnowsTreeByIds(Long[] ids)
    {
        return knowsTreeMapper.deleteKnowsTreeByIds(ids);
    }

    /**
     * 删除知识点树信息
     * 
     * @param id 知识点树主键
     * @return 结果
     */
    @Override
    public int deleteKnowsTreeById(Long id)
    {
        return knowsTreeMapper.deleteKnowsTreeById(id);
    }

    /**
     * 查询知识点及关联的题目
     * @return
     */
    @Override
    public List<KnowQuestionVO> selectTreeQuestion(String knowName) {

        List<KnowsIdAndQuestionIdVO> knowsTrees = knowsTreeMapper.selectKnowsQuestionList(knowName);
        List<KnowQuestionVO> kqList = knowsTrees.stream().map(item -> {
            // 返回实体
            KnowQuestionVO knowQuestionVO = new KnowQuestionVO();
            Long kid = item.getKId();
            knowQuestionVO.setKnowId(kid);
            knowQuestionVO.setKnowName(item.getKName());
            knowQuestionVO.setParentId(item.getParentId());
            // 查询关联表，获取题目id
            String qIds = item.getQIds();
            String[] qIdArr = qIds.split(",");
            // 根据题目id获取题目题干
            List<KnowAndQuestionVO> kaqList = Arrays.stream(qIdArr).map(qId -> {
                    String stem = questionMapper.selectStemsById(Long.valueOf(qId));
                    KnowAndQuestionVO knowAndQuestionVO = new KnowAndQuestionVO(Long.valueOf(qId), stem);
                    return knowAndQuestionVO;
            }).collect(Collectors.toList());
            knowQuestionVO.setQuestionList(kaqList);
            return knowQuestionVO;
        }).collect(Collectors.toList());

        return kqList;
    }

    /**
     * 知识点树递归
     * @return
     */
    public List<KnowQuestionVO> getTreeQuestion() {

        // 缓存进redis
        List<KnowQuestionVO> cacheList = redisService.getCacheList(XZSContent.KEY_TREE_QUESTION);
        if (null == cacheList|| cacheList.size() <= 0){

            // 查询所有的知识点树及所包含题目
            List<KnowQuestionVO> kqList = getAllWithTreeAndQuestion();

            // 递归
            List<KnowQuestionVO> treeList = kqList.stream().filter(kqVo -> {
                return kqVo.getParentId() == 0;
            }).map(item -> {
                item.setChildren(getKnowQuestionVOChild(item.getKnowId(),kqList));
                return item;
            }).sorted((vo1, vo2) -> {
                return (int) (vo1.getKnowId() - vo2.getKnowId());
            }).collect(Collectors.toList());

            // 缓存进redis
            redisService.setCacheList(XZSContent.KEY_TREE_QUESTION,treeList);
            return treeList;

        }

        return cacheList;
    }

    /**
     * 查询树的子类
     * @param knowId
     * @param kqList
     * @return
     */
    private List<KnowQuestionVO> getKnowQuestionVOChild(Long knowId, List<KnowQuestionVO> kqList) {

        // 递归
        List<KnowQuestionVO> treeList = kqList.stream().filter(kqVo -> {
            return kqVo.getParentId() == knowId;
        }).map(item -> {
            item.setChildren(getKnowQuestionVOChild(item.getKnowId(),kqList));
            return item;
        }).sorted((vo1, vo2) -> {
            return (int) (vo1.getKnowId() - vo2.getKnowId());
        }).collect(Collectors.toList());

        return treeList;
    }

    /**
     * 查询所有的知识点树及所包含题目
     * @return
     */
    @Override
    public List<KnowQuestionVO> getAllWithTreeAndQuestion(){

        List<KnowQuestionVO> list = knowsTreeMapper.getAllWithTreeAndQuestion();

        return list;
    }

}

